package cn.tcmp.controller;

import cn.tcmp.entity.Detail;
import cn.tcmp.service.DetailService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class DetailController {

    @Autowired
    private DetailService detailService;

    @RequestMapping(value = "queryAll", method = RequestMethod.GET
            , produces = {"application/json;charset=utf-8"})
    public String queryAll(@RequestParam("sortId") Integer sortId) {
        return JSON.toJSONString(detailService.queryAll(sortId));
    }

    @RequestMapping(value = "save", method = RequestMethod.GET)
    public String save(@RequestParam("sortId") Integer sortId,
                       @RequestParam("author") String author,
                       @RequestParam("title") String title,
                       @RequestParam("detail") String detail) {
        Detail deta = new Detail();
        deta.setSortId(sortId);
        deta.setAuthor(author);
        deta.setTitle(title);
        deta.setDetail(detail);
        deta.setCreateDate(new Date());
        deta.setReplyCount(0);
        if (detailService.save(deta) > 0) {
            return "success";
        }
        return "error";
    }


}
